/**
 * 
 */
package com.hls;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

/**
 * @author huangliusong 
 * E-mail:huangliusong1994@gmail.com
 * @version 创建时间：2018年7月10日 下午12:03:14
 */
public class ExecLinuxCMD {
	public static Object exec(String cmd) {
		try {
			String[] cmdA = { "/bin/sh", "-c", cmd };
			Process process = Runtime.getRuntime().exec(cmdA);
			LineNumberReader br = new LineNumberReader(new InputStreamReader(
					process.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String line;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
				sb.append(line).append("\n");
			}
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String pwdString = exec("pwd").toString();
		String netsString = exec("netstat -nat|grep -i \"80\"|wc -l")
				.toString();

		System.out.println("==========获得值=============");
		System.out.println(pwdString);
		System.out.println(netsString);
	}
}
